package o;

import android.content.Context;
import android.content.Intent;
import android.content.pm.PackageInfo;
import android.content.pm.PackageManager;
import android.content.pm.PackageManager.NameNotFoundException;
import android.os.AsyncTask;
import android.os.Bundle;
import android.securityprofile.SecurityProfileManager;
import android.text.TextUtils;
import com.huawei.appgallery.downloadengine.api.SessionDownloadTask;
import com.huawei.appgallery.foundation.store.bean.installresult.ReportInstallResultResBean;
import com.huawei.appgallery.serverreqkit.api.bean.ResponseBean;
import com.huawei.appgallery.serverreqkit.api.listener.IServerCallBack;
import com.huawei.appmarket.service.installresult.dao.InstallResultCache;
import com.huawei.appmarket.support.storage.referrer.AppReferrerRecord;
import com.huawei.hms.framework.network.restclient.hwhttp.dns.DnsConfig;
import com.huawei.hms.support.api.entity.pay.HwPayConstant;
import com.huawei.hwid.core.constants.HwAccountConstants;
import java.io.RandomAccessFile;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.Locale;
import java.util.concurrent.atomic.AtomicInteger;
import o.cfz.e;
import o.epx.b;

public class eba implements IServerCallBack {
    public final AtomicInteger ˊ = new AtomicInteger();
    private IServerCallBack ˋ;
    private eum ˏ = null;

    public static eba ˋ() {
        return new eba();
    }

    public static eba ॱ(IServerCallBack iServerCallBack) {
        eba o_eba = new eba();
        o_eba.ˋ = iServerCallBack;
        return o_eba;
    }

    public void ˏ(Context context, String str, Object obj, int i, boolean z) {
        ˏ(context, str, obj, i, z, context.getPackageName());
    }

    public void ˏ(Context context, String str, Object obj, int i, boolean z, String str2) {
        int i2 = 1;
        Logger.ˎ("ReportInstallResult", "ReportInstallResultTask execute start");
        if (3 == i && !ebc.ˊ().ˋ()) {
            return;
        }
        if (TextUtils.isEmpty(str)) {
            Logger.ˋ("ReportInstallResult", "ReportInstallResultTask execute packageName is null.");
            return;
        }
        cfy o_cfy = new cfy();
        dsz.ˋ(str);
        if (3 != i) {
            o_cfy.ʻ(ॱ(str));
        }
        o_cfy.ˎ(str);
        o_cfy.ˏ(0);
        o_cfy.ॱ(i);
        o_cfy.ˊॱ(str2);
        if (azc.ˎ().ॱ() >= 17) {
            o_cfy.ॱॱ(ˎ(str));
        }
        if (obj instanceof SessionDownloadTask) {
            ˊ((SessionDownloadTask) obj, o_cfy);
        }
        try {
            PackageInfo packageInfo = ContentManager.getInstant().getContext().getPackageManager().getPackageInfo(str, DnsConfig.MAX_CACHE_ENTRIES);
            if (packageInfo != null) {
                o_cfy.ˎ(packageInfo.versionCode);
                o_cfy.ˊ(erz.ˏ(packageInfo));
            }
            if (!(3 != i || packageInfo == null || packageInfo.lastUpdateTime == packageInfo.firstInstallTime)) {
                z = true;
            }
        } catch (NameNotFoundException e) {
            Logger.ˏ("ReportInstallResult", "can not get versionCode:" + str);
        }
        if (z) {
            i2 = 0;
        }
        o_cfy.ˋ(i2);
        ˊ(obj, o_cfy);
        cfw.ˎ(o_cfy, this);
    }

    private void ˊ(SessionDownloadTask sessionDownloadTask, cfy o_cfy) {
        o_cfy.ॱ(sessionDownloadTask.ʾ());
        o_cfy.ᐝ(sessionDownloadTask.ॱͺ());
        o_cfy.ॱˊ(sessionDownloadTask.ॱʼ());
        o_cfy.ʼ(sessionDownloadTask.ॱˋ());
        o_cfy.setServiceType_(sessionDownloadTask.ॱ());
        o_cfy.ˊ(cej.ˊ(sessionDownloadTask.ᐝ(), "aId"));
        o_cfy.ॱॱ(sessionDownloadTask.ॱॱ());
        o_cfy.ʽ(ˏ(sessionDownloadTask));
        o_cfy.ˊ(sessionDownloadTask.ˋ());
        o_cfy.ˏॱ(sessionDownloadTask.ˊ());
        o_cfy.ˋॱ(sessionDownloadTask.ॱˊ());
        String ˏ = dof.ˏ(sessionDownloadTask.ᐝ(), "source");
        if (!TextUtils.isEmpty(sessionDownloadTask.ᐨ())) {
            eue.ˎ().ॱ(sessionDownloadTask.ˊˊ(), sessionDownloadTask.ᐨ(), sessionDownloadTask.ॱˋ());
        }
        if (!TextUtils.isEmpty(sessionDownloadTask.ॱˊ())) {
            eug.ˏ().ˎ(sessionDownloadTask.ˊˊ(), sessionDownloadTask.ॱˊ());
        }
        if (ˏ == null) {
            ˏ = "";
        }
        o_cfy.setSource_(ˏ);
        if (sessionDownloadTask.ˌ() == 4) {
            o_cfy.setBlockIfProtocolNotAgreed(false);
        }
        ˎ(sessionDownloadTask, o_cfy);
    }

    private void ˎ(SessionDownloadTask sessionDownloadTask, cfy o_cfy) {
        String ॱˎ = sessionDownloadTask.ॱˎ("trackId");
        String ॱˎ2 = sessionDownloadTask.ॱˎ("referrer");
        if (ॱˎ != null && !ॱˎ.equals(HwAccountConstants.NULL)) {
            eun.ˏ().ˎ(new AppReferrerRecord(o_cfy.ॱ(), sessionDownloadTask.ॱˎ("mediaPkg"), ॱˎ, sessionDownloadTask.ˋˋ() / 1000, System.currentTimeMillis() / 1000));
        } else if (ॱˎ2 == null || ॱˎ2.equals(HwAccountConstants.NULL)) {
            Logger.ˋ("ReportInstallResult", "referrer info is not exist");
            ॱˎ = null;
        } else {
            eun.ˏ().ˎ(new AppReferrerRecord(o_cfy.ॱ(), sessionDownloadTask.ॱˎ("mediaPkg"), ॱˎ2, sessionDownloadTask.ˋˋ() / 1000, System.currentTimeMillis() / 1000));
            ॱˎ = ॱˎ2;
        }
        String ॱˎ3 = sessionDownloadTask.ॱˎ("familyShare");
        if (ॱˎ3 != null) {
            o_cfy.ˏ(ॱˎ3);
        }
        ॱˎ3 = sessionDownloadTask.ॱˎ(HwPayConstant.KEY_PRODUCT_NO);
        if (ॱˎ3 != null) {
            eed.ˊ(ॱˎ3, sessionDownloadTask.ˊˊ(), 0, sessionDownloadTask.ॱ());
        }
        ॱˎ3 = sessionDownloadTask.ॱˎ("advPlatform");
        Logger.ˎ("ReportInstallResult", "advPlatform=" + ॱˎ3);
        if (String.valueOf(1).equals(ॱˎ3)) {
            ˋ(o_cfy.ॱ(), dof.ˊ(ॱˎ));
        }
    }

    private void ˋ(String str, String str2) {
        if (this.ˏ == null) {
            this.ˏ = new eum(str, str2);
            Intent intent = new Intent("com.huawei.android.hms.CHANNEL_SERVICE");
            intent.setPackage("com.huawei.hwid");
            ContentManager.getInstant().getContext().bindService(intent, this.ˏ, 1);
            return;
        }
        this.ˊ.incrementAndGet();
        this.ˏ.ˋ(str, str2);
    }

    public void ˊ() {
        if (this.ˊ.get() <= 0) {
            if (this.ˏ != null) {
                ContentManager.getInstant().getContext().unbindService(this.ˏ);
            }
            this.ˏ = null;
        }
    }

    private void ˊ(Object obj, cfy o_cfy) {
        for (e ˎ : cfz.ˏ()) {
            ˎ.ˎ(o_cfy, obj);
        }
    }

    private String ˎ(String str) {
        String str2 = null;
        try {
            str2 = dmb.ॱ(dmj.ˎ(new RandomAccessFile(ContentManager.getInstant().getContext().getPackageManager().getPackageInfo(str, 0).applicationInfo.sourceDir, "r")));
        } catch (Exception e) {
            Logger.e("ReportInstallResult", e.toString());
        }
        return str2;
    }

    private String ˏ(SessionDownloadTask sessionDownloadTask) {
        return dof.ˏ(sessionDownloadTask.ᐝ(), "channelNo");
    }

    private String ॱ(String str) {
        return eny.ˊ().ˏ(str);
    }

    public void prePostResult(BaseRequest o_cvu, ResponseBean responseBean) {
        if (this.ˋ != null) {
            this.ˋ.prePostResult(o_cvu, responseBean);
        }
    }

    public void notifyResult(BaseRequest o_cvu, ResponseBean responseBean) {
        int i;
        if (this.ˋ != null) {
            this.ˋ.notifyResult(o_cvu, responseBean);
        }
        final cfy o_cfy = (cfy) o_cvu;
        for (e ˋ : cfz.ˏ()) {
            ˋ.ˋ(o_cfy, responseBean);
        }
        if (responseBean.getResponseCode() != 0) {
            i = 1;
        } else if (responseBean instanceof ReportInstallResultResBean) {
            ReportInstallResultResBean reportInstallResultResBean = (ReportInstallResultResBean) responseBean;
            Logger.ˎ("ReportInstallResult", reportInstallResultResBean.toString());
            if (reportInstallResultResBean.getRtnCode_() == 0) {
                if (!("0".equals(reportInstallResultResBean.ˊ()) || dof.ˋ(reportInstallResultResBean.ˊ()))) {
                    Bundle bundle = new Bundle();
                    bundle.putString("gain_points", reportInstallResultResBean.ˊ());
                    bundle.putString("user_points", reportInstallResultResBean.ॱ());
                    bundle.putString("pkgName", o_cfy.ॱ());
                    Intent intent = new Intent();
                    intent.putExtras(bundle);
                    intent.setAction(b.ˊ);
                    ll.ˏ(ContentManager.getInstant().getContext()).ˋ(intent);
                }
                ˋ(reportInstallResultResBean.ˏ());
                dsz.ˊ(o_cfy.ॱ(), reportInstallResultResBean.ˎ());
                i = 0;
            } else {
                i = 1;
            }
        } else {
            i = 0;
        }
        if (i != 0) {
            new AsyncTask<Object, Void, Void>(this) {
                final /* synthetic */ eba ˊ;

                protected /* synthetic */ Object doInBackground(Object[] objArr) {
                    return ˏ(objArr);
                }

                protected Void ˏ(Object... objArr) {
                    this.ˊ.ˋ(o_cfy);
                    return null;
                }
            }.execute(new Object[0]);
        }
    }

    private void ˋ(String str) {
        try {
            if (TextUtils.isEmpty(str)) {
                Logger.ˎ("ReportInstallResult", "GreenSignature is empty!");
                return;
            }
            byte[] bytes = str.getBytes("UTF-8");
            SecurityProfileManager securityProfileManager = SecurityProfileManager.getDefault();
            Bundle bundle = new Bundle();
            bundle.putByteArray("addDomainPolicy", bytes);
            Bundle hwSignedInfoToSEAPP = securityProfileManager.setHwSignedInfoToSEAPP(bundle);
            if (hwSignedInfoToSEAPP == null) {
                return;
            }
            if (hwSignedInfoToSEAPP.getInt("RESULT_addDomainPolicy", -1) == 0) {
                Logger.ˎ("ReportInstallResult", "Add domain policy successfully!");
            } else {
                Logger.ˎ("ReportInstallResult", "Failed to add domain policy!");
            }
        } catch (Exception e) {
            Logger.e("ReportInstallResult", "Failed to add domain policy: " + e.getMessage());
        } catch (NoSuchMethodError e2) {
            Logger.e("ReportInstallResult", "No such method : " + e2.getMessage());
        }
    }

    private void ˋ(cfy o_cfy) {
        PackageManager packageManager = ContentManager.getInstant().getContext().getPackageManager();
        if (packageManager != null) {
            PackageInfo packageInfo;
            try {
                packageInfo = packageManager.getPackageInfo(o_cfy.ॱ(), DnsConfig.MAX_CACHE_ENTRIES);
            } catch (NameNotFoundException e) {
                Logger.ˋ("ReportInstallResult", "saveInstallRecord exception: " + e.toString());
                packageInfo = null;
            }
            if (packageInfo != null) {
                CharSequence ʽ;
                String num = Integer.toString(packageInfo.versionCode);
                InstallResultCache installResultCache = new InstallResultCache();
                installResultCache.ˎ(o_cfy.ॱ());
                installResultCache.ˏ(o_cfy.ˎ());
                installResultCache.ॱॱ(o_cfy.ʽ());
                installResultCache.ॱ(new SimpleDateFormat("yyyy-MM-dd hh:mm:ss", Locale.US).format(new Date()));
                installResultCache.ˋ(num);
                installResultCache.ʼ(o_cfy.ˏॱ());
                installResultCache.ᐝ(o_cfy.ॱˊ());
                installResultCache.ˏ(o_cfy.ͺ());
                installResultCache.ॱ(o_cfy.ʻ());
                installResultCache.ˎ(o_cfy.ʼ());
                installResultCache.ˎ(o_cfy.ॱॱ());
                installResultCache.ʽ(o_cfy.getSource_());
                installResultCache.ˋ(o_cfy.ˊ());
                installResultCache.ˊ(o_cfy.ˊॱ());
                eaz ˏ = eaz.ˏ();
                List<InstallResultCache> ˊ = ˏ.ˊ("pkgName = ? and versionCode = ?", new String[]{installResultCache.ˊ(), installResultCache.ˋ()});
                String str = "";
                if (!(ˊ == null || ˊ.isEmpty())) {
                    for (InstallResultCache installResultCache2 : ˊ) {
                        if (ˋ(installResultCache, installResultCache2)) {
                            ʽ = installResultCache2.ʽ();
                            installResultCache.ॱ((String) ʽ);
                            break;
                        }
                    }
                }
                Object obj = str;
                if (TextUtils.isEmpty(ʽ)) {
                    Logger.ˎ("ReportInstallResult", "insert result:" + ˏ.ˎ(installResultCache));
                    return;
                }
                Logger.ˎ("ReportInstallResult", "update result:" + ˏ.ॱ(installResultCache, "pkgName = ? and versionCode = ? and installTime = ?", new String[]{installResultCache.ˊ(), installResultCache.ˋ(), installResultCache.ʽ()}));
            }
        }
    }

    private boolean ˋ(InstallResultCache installResultCache, InstallResultCache installResultCache2) {
        return ˎ(installResultCache2.ॱ(), installResultCache.ॱ()) && installResultCache2.ᐝ() == installResultCache.ᐝ() && installResultCache2.ʼ() == installResultCache.ʼ() && installResultCache2.ॱॱ() == installResultCache.ॱॱ() && ˎ(installResultCache2.ˊ(), installResultCache.ˊ()) && ˎ(installResultCache2.ʻ(), installResultCache.ʻ()) && installResultCache2.ˎ() == installResultCache.ˎ();
    }

    private boolean ˎ(String str, String str2) {
        if (str == null) {
            return str2 == null;
        } else {
            return str.equals(str2);
        }
    }
}
